//虚拟供应商管理-inventedVendor.js


// 设置领域模型必填
function setRequired(dataSource) {
    // 需要 必填的字段
    var requiredField = ["company_name", "major_product_category"];
    var filterProperties = a => requiredField.includes(a.code);
    var setRequired = a => a.required = true;

    dataSource.schema.properties.filter(filterProperties).forEach(setRequired);
    dataSource.newItem();
}


//打开新增虚拟组织加盟弹框
function opentVendorJoinAdd() {
    var dataSource = h3ui.models.t_vendor_join;

    setRequired(dataSource);

    dataSource.item.architecture_type = "1";
    dataSource.item.account_period = 0;
    dataSource.item.registered_capital = 0;
    dataSource.item.type = 2;
    dataSource.item.join_status = 1;
    //给系统字段赋值
    var user = JSON.parse(localStorage.getItem("user"));
    dataSource.item.createdBy = user.userInfo.id;
    dataSource.item.createdDept = user.userInfo.id;
    dataSource.item.createdTime = new Date();
    dataSource.item.owner = user.userInfo.id;
    dataSource.item.ownerDept = user.userInfo.id;
    dataSource.item.status = "finished";


    h3ui.widgets.CustomModal2.props.visible = true;
    h3ui.widgets.CustomModal2.props.title = "添加供应商";
}


//提交组织加盟
function submitVendorJoin() {
    var dataSource = h3ui.models.t_vendor_join;
    if (!dataSource.validate()) {
        return;
    }

    dataSource.submitBO().then(res => {
        h3ui.$message.info("提交成功");
        reloadList("FormList4");
        h3ui.widgets.CustomModal2.props.visible = false;
    });

}


//打开修改修改虚拟组织加盟弹框
function openVendorJoinMidfy() {
    var rows = h3ui.widgets.FormList4.props.selectedRow;
    if (rows.length == 0) {
        h3ui.$message.info("请勾选一行数据");
        return;
    }
    if (rows.length > 1) {
        h3ui.$message.info("仅能勾选一行数据");
        return;
    }

    var row = rows[0];

    var dataSource = h3ui.models.t_vendor_join;
    setRequired(dataSource);

    var data = JSON.parse(JSON.stringify(row));

    if (data.license) {
        data.license.forEach(b => {
            b.uid = b.id;
            b.status = "done";
            b.url = "/api/file/download?refId=" + b.id;
        })
    }

    dataSource.item = data;

    h3ui.widgets.CustomModal2.props.visible = true;
    h3ui.widgets.CustomModal2.props.title = "修改供应商";
}


// 打开虚拟供应商加盟审批弹框
function openVendorJoinApproval() {
    var rows = h3ui.widgets.FormList4.props.selectedRow;
    if (rows.length == 0) {
        h3ui.$message.info("请勾选一行数据");
        return;
    }
    if (rows.length > 1) {
        h3ui.$message.info("仅能勾选一行数据");
        return;
    }

    var row = rows[0];

    var dataSource = h3ui.models.t_vendor_join;
    dataSource.newItem();

    var data = JSON.parse(JSON.stringify(row));

    dataSource.item = data;

    h3ui.widgets.FormModel4.props.editMode = false;
    h3ui.widgets.CustomModal3.props.visible = true;

}


//审批通过
function sumbmitApproval() {

    var url = "/api/launcher/approval/application/invoke?action=/VendorJoinService/approve";

    var dataSource = h3ui.models.t_vendor_join;

    var data = h3ui.models.t_vendor_join.item;

    data = JSON.parse(JSON.stringify(data));

    if (data.major_product_category && data.major_product_category instanceof Array && data.major_product_category.length > 0) {
        data.major_product_category = data.major_product_category[0].name;
    }


    h3ui.axios({
        method: 'post',
        url,
        data: data,
        headers: {
            Authorization: 'Bearer ' + localStorage.getItem("token")
        }
    }).then(function (res) {
        console.log(res);

        if (res.status == 200) {
            if (res.data.success) {
                h3ui.$message.info('提交成功');
                reloadList("FormList4");

                h3ui.widgets.CustomModal3.props.visible = false;


            } else {
                h3ui.$message.error(res.errMessage || '提交失败');
            }

        } else {
            h3ui.$message.error('提交失败');
        }

    }).catch(function (error) {
        h3ui.$message.error(error || '提交失败');
    });

}

//拒绝审批
function rejectApprove() {
    var comment = h3ui.widgets.Textarea3.props.value;
    if (!comment) {
        h3ui.$message.info("请填写拒绝理由!");
        return false;
    }

    var id = h3ui.models.t_vendor_join.item.id;
    var data = {id, comment};

    h3ui.services.rejectApprove({data}).then(res => {
        if (res.success) {
            h3ui.$message.info("提交成功");
            h3ui.widgets.CustomModal3.props.visible = false;

            reloadList("FormList4");
        }
    })

}


//===========================绑定虚拟供应商==============


// 打开绑定供应商弹框
function openVendorModal() {

    var dataSource = h3ui.models.t_binding_invented_vendor;

    // 需要 必填的字段
    var requiredField = ["organization_id"];
    var filterProperties = a => requiredField.includes(a.code);
    var setRequired = a => a.required = true;
    dataSource.schema.properties.filter(filterProperties).forEach(setRequired);

    var currentUser = JSON.parse(localStorage.getItem("user")).userInfo;

    dataSource.newItem();

    dataSource.item.binder_id = [currentUser];
    dataSource.item.user_name = currentUser.name;

    h3ui.widgets.CustomModal1.props.visible = true;


}

//提交绑定供应商
function submitVendor() {

    var dataSource = h3ui.models.t_binding_invented_vendor;

    if (!dataSource.validate()) {
        return false;
    }

    var organization_id = dataSource.item.organization_id;

    if (organization_id != null && organization_id.length > 0) {
        dataSource.item.organization_name = organization_id[0].name;
    }

    dataSource.submitBO().then(res => {
        h3ui.$message.info("绑定成功");
        reloadList("FormList3");
        h3ui.widgets.CustomModal1.props.visible = false;
    })

}

//重设
function reSetItem() {
    h3ui.models.t_binding_invented_vendor.newItem();
}


//解绑供应商品
function unbindingVendor() {
    var keys = h3ui.widgets.FormList3.props.selectedRowKeys;

    if (keys.length == 0) {
        h3ui.$message.info("请勾选一行数据");
        return;
    }


    h3ui.$confirm({
        title: '确认删除选择的数据吗',
        onOk() {
            h3ui.models.t_binding_invented_vendor.batchDeleteBO({bizObjectIds: keys}).then(res => {
                h3ui.$message.info("删除成功");
                reloadList("FormList3");
            })
        }
    });


}


//触发列表刷新事件
function reloadList(fromListId) {
    doReloadList({pageIndex: 1, node: {id: fromListId}});
}

//列表刷新
function doReloadList(evt) {
    var list = h3ui.widgets[evt.node.id];

    var queryId = list.childIds.find(a => a.includes("ModelQueryForm"));

    var filter = h3ui.widgets[queryId].props.value || {};

    filter = JSON.parse(JSON.stringify(filter));

    var pageId = list.childIds.find(a => a.includes("Pagination"));

    var pageIndex = h3ui.widgets[pageId].props.current;
    var pageSize = h3ui.widgets[pageId].props.pageSize;


    list.emit("load", {pageIndex, pageSize, filter});


    list.props.selectedRowKeys = [];
    list.props.selectedRow = [];

}